package com.shujia.mapreduce;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;

public class Demo2SumScore {

    public static class SumScoreMapper extends Mapper<LongWritable, Text, Text, IntWritable> {
        @Override
        protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, IntWritable>.Context context) throws IOException, InterruptedException {

            //将一行数据转换成字符串
            String line = value.toString();

            //以 , 切分字符串
            String[] split = line.split(",");

            //获取学生的id
            String id = split[0];

            //获取学生每一科的分数
            int score = Integer.parseInt(split[2]);

            //将id,score输出到reduce端
            context.write(new Text(id), new IntWritable(score));

        }
    }

    public static class SumScoreReducer extends Reducer<Text, IntWritable, Text, NullWritable> {

        /**
         * Iterable ； 迭代器，是一个可以循环迭代的对象，数据可能在内存中，也可能在磁盘上
         */
        @Override
        protected void reduce(Text key, Iterable<IntWritable> values, Reducer<Text, IntWritable, Text, NullWritable>.Context context) throws IOException, InterruptedException {

            //遍历每一科的分数
            int sum = 0;
            for (IntWritable value : values) {
                //每一科的分数
                int score = value.get();
                //对每一科的分数进行累加
                sum += score;
            }

            //key是学生的id，将其转换成字符串，并在后面拼接每个学生的总分
            String sumScore = key.toString() + "," + sum;

            //将数据输出到hdfs
            context.write(new Text(sumScore), NullWritable.get());

        }
    }

    public static void main(String[] args) throws Exception {

        //hadoop 配置文件对象
        Configuration configuration = new Configuration();

        //指定切片的最大值
        configuration.set("mapreduce.input.fileinputformat.split.maxsize", "66560");

        Job job = Job.getInstance(configuration);

        job.setNumReduceTasks(1);

        job.setJobName("sumscore");

        job.setJarByClass(Demo2SumScore.class);

        job.setMapperClass(SumScoreMapper.class);
        job.setMapOutputValueClass(Text.class);
        job.setMapOutputValueClass(IntWritable.class);

        job.setReducerClass(SumScoreReducer.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(NullWritable.class);

        FileInputFormat.addInputPath(job, new Path("/data/score"));

        //如果输出路径已存在，先删除
        Path outPath = new Path("/data/score_sum");
        FileSystem fileSystem = FileSystem.get(new Configuration());
        if (fileSystem.exists(outPath)) {
            fileSystem.delete(outPath, true);
        }

        FileOutputFormat.setOutputPath(job, new Path("/data/score_sum"));

        job.waitForCompletion(true);
        /**
         * 提交任务
         * hadoop jar hadoop-1.0-SNAPSHOT.jar com.shujia.mapreduce.Demo2SumScore
         *
         */

    }
}
